<template>
  <div class="register-container">
    <el-card class="register-card">
      <template #header>
        <div class="card-header">
          <span>电力设备智能管理系统注册</span>
        </div>
      </template>
      <el-form :model="registerForm" :rules="rules" ref="registerFormRef" label-width="0px">
        <el-form-item prop="name">
          <el-input v-model="registerForm.name" placeholder="请输入用户名(重名会导致登录失败)" :prefix-icon="UserIcon" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" v-model="registerForm.password" placeholder="请输入密码" :prefix-icon="Lock" show-password />
        </el-form-item>
        <el-form-item prop="confirmPassword">
          <el-input type="password" v-model="registerForm.confirmPassword" placeholder="请确认密码" :prefix-icon="Lock" show-password />
        </el-form-item>
        <el-form-item prop="role">
          <el-select v-model="registerForm.role" placeholder="请选择角色" style="width: 100%;">
            <el-option label="设备管理人员" value="1" />
            <el-option label="维修人员" value="2" />
            <el-option label="普通用户" value="3" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :loading="loading" @click="handleRegister" style="width: 100%;">注册</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="text" @click="$router.push('/login')" style="width: 100%; text-align: center;">已有账号？去登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import { useAuthStore } from '@/stores/modules/auth';

const router = useRouter();
const store = useAuthStore();
const registerFormRef = ref(null);
const loading = ref(false);

const registerForm = reactive({
  name: '',
  password: '',
  confirmPassword: '',
  role: ''
});

const validatePass = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请输入密码'));
  } else {
    if (registerForm.confirmPassword !== '') {
      registerFormRef.value.validateField('confirmPassword');
    }
    callback();
  }
};

const validatePass2 = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请再次输入密码'));
  } else if (value !== registerForm.password) {
    callback(new Error('两次输入的密码不一致'));
  } else {
    callback();
  }
};

const rules = reactive({
  name: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, validator: validatePass, trigger: 'blur' }],
  confirmPassword: [{ required: true, validator: validatePass2, trigger: 'blur' }],
  role: [{ required: true, message: '请选择角色', trigger: 'change' }]
});

const handleRegister = () => {
  registerFormRef.value.validate((valid) => {
    if (valid) {
      loading.value = true;
      store.register(registerForm)
        .then((response) => {
          // ElMessage.success('注册成功，请登录');
          router.push('/login');
        })
        .catch((error) => {
          ElMessage.error('注册失败，请重试');
        })
        .finally(() => {
          loading.value = false;
        });
    } else {
      ElMessage.error('表单验证失败，请检查输入');
    }
  });
};
</script>

<style lang="scss" scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f0f2f5;

  .register-card {
    width: 400px;
    padding: 20px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);

    .card-header {
      text-align: center;
      font-size: 20px;
      font-weight: bold;
      margin-bottom: 20px;
    }
  }
}
</style>
